/* @(#)LocalDatabaseRowLoader.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.flywheelms.library.fmm.database.sqlite;

import java.io.BufferedReader;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import android.database.sqlite.SQLiteDatabase;

import com.flywheelms.library.fmm.helper.FmmAssetsHelper;
import com.flywheelms.library.fmm.helper.FmmSqlHelper;
import com.flywheelms.library.fmm.node.impl.enumerator.FmmNodeDictionary;
import com.flywheelms.library.fmm.node.interfaces.horizontal.FmmNode;

public class LocalDatabaseRowLoader {
	
	private static LocalDatabaseRowLoader singleton = null;

	public LocalDatabaseRowLoader() {
	}
	
	public static LocalDatabaseRowLoader getInstance() {
		if(LocalDatabaseRowLoader.singleton == null){
			LocalDatabaseRowLoader.singleton = new LocalDatabaseRowLoader();
		}
		return LocalDatabaseRowLoader.singleton;
	}

	public static boolean insertRows(SQLiteDatabase aSQLiteDatabase, String aRowsDirectoryName) {
		for(Class<? extends FmmNode> theNodeClass : getTablesToLoad(aRowsDirectoryName)) {
			initializeTableFromCsvAsset(aSQLiteDatabase, getRowsDirectoryPath(aRowsDirectoryName), theNodeClass);
		}
		return true;
	}

	private static ArrayList<Class<? extends FmmNode>> getTablesToLoad(String aRowsDirectoryName) {
		String[] theFileList = FmmAssetsHelper.getFileNameList(getRowsDirectoryPath(aRowsDirectoryName));
		ArrayList<Class<? extends FmmNode>> theTablesToLoad = new ArrayList<Class<? extends FmmNode>>();
		for(FmmNodeDictionary theNodeEntry : FmmNodeDictionary.DATABASE_LOAD_ORDER) {
			if(fileExistsForNode(theFileList, theNodeEntry)) {
				theTablesToLoad.add(theNodeEntry.getNodeClass());
			}
		}
		return theTablesToLoad;
	}
	
	private static boolean fileExistsForNode(String[] theFileList, FmmNodeDictionary theNodeEntry) {
		for(String theFileName : Arrays.asList(theFileList)) {
			if(theFileName.split("\\.")[0].equals(theNodeEntry.getName())) {
				return true;
			}
		}
		return false;
	}

//	@SuppressWarnings("unused")
//	private void initializeTableFromXmlAsset(Class<? extends FmmNode> theNodeClass) {
//		XmlPullParser thePullParser = FmmAssetsHelper.getXmlParserForAsset(getRowsFilePath(theNodeClass));
//		String theClassName = theNodeClass.getSimpleName();
//		try {
//			int eventType = thePullParser.getEventType();
//			String theTagName;
//			while(eventType != XmlPullParser.END_DOCUMENT) {
//				if(eventType == XmlPullParser.START_DOCUMENT) {
//
//				}
//				else if(eventType == XmlPullParser.START_TAG) {
//					theTagName = thePullParser.getName();
//					if(theTagName.equals(theClassName)) {
//						FmmSqlHelper.insertRowFromXmlParser(theClassName, thePullParser, this.sqLiteDatabase);
//					}
//				}
//				else if(eventType == XmlPullParser.END_TAG) {
//				}
//				else if(eventType == XmlPullParser.TEXT) {
//				}
//				eventType = thePullParser.next();
//			}
//		} catch (XmlPullParserException e) {
//			System.out.print(e.toString());
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

	public static String getRowsDirectoryPath(String aRowsDirectoryName) {
		return "database" + File.separator + "rows" + File.separator + aRowsDirectoryName;
	}
	
	public static String getRowsFilePath(String aRowsDirectoryPath, Class<? extends FmmNode> aClass) {
		return aRowsDirectoryPath + File.separator + aClass.getSimpleName() + ".rows";
	}

	private static void initializeTableFromCsvAsset(SQLiteDatabase aSQLiteDatabase, 
			String aRowsDirectoryPath, Class<? extends FmmNode> theNodeClass ) {
		BufferedReader theBufferedReader = FmmAssetsHelper.getBufferedReader(getRowsFilePath(aRowsDirectoryPath, theNodeClass));
		FmmSqlHelper.insertRowsFromCsvFile(theNodeClass.getSimpleName(), theBufferedReader, aSQLiteDatabase);
	}

}
